package com.bo.baby2.config;

import com.bo.baby2.entity.TUserAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * @program: baby2_p2p
 * @description: session过滤器
 * @author:波
 * @create: 2020-07-14 09:22
 **/
/*@Component*/
public class FilterSession implements Filter {
    private static final Logger log= LoggerFactory.getLogger(FilterSession.class);
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        TUserAccount userAccount =(TUserAccount) request.getSession().getAttribute("userAccount");
        //获取请求路径
        String requestURI = request.getRequestURI();
        //设置不过滤请求
        List<String> urls = Arrays.asList("/index.html", "/","/tpl-head.html","/tpl-navbar.html","/tpl-footer.html","/finance/borrow/query","/login.html","/user/login"
        ,"/register.html","/user/checkUsername","/user/register","admin/login.html");
        //如果请求路径包含设置的不过滤路径就放行
        if(urls.contains(requestURI)||requestURI.contains("js")||requestURI.contains("css")||requestURI.contains("images")){
            filterChain.doFilter(request,response);
        } else if(userAccount!=null){
            filterChain.doFilter(request,response);
        }else {
            log.warn("过滤路径"+requestURI);
            if(requestURI.contains("admin")){
                response.sendRedirect("admin/login.html");
            }else {
                response.sendRedirect("index.html");
            }

        }
    }
}
